#!/usr/bin/env python
""" Adds links in the manual pages to tutorials that utilize the functions"""

import os

def processfile(petsc_dir,dir,file,keyre,mdict,uses):
  '''Find all functions used in the tutorial and add links to the manual page for the function'''
  #print('Processing '+os.path.join(dir,file))
  with open(os.path.join(dir,file),'r') as fd:
    text = fd.read()
  found = list(set(keyre.findall(text)))
  for i in found:
    if len(uses[i]) < 10:
      uses[i].append(os.path.join(dir,file))

def processdir(petsc_dir,dir,keyre,mdict,uses):
  '''Loop over tutorials, call processfile() on each'''
  #print('Processing '+dir)
  for file in os.listdir(dir):
    if os.path.isfile(os.path.join(dir,file)) and (file.endswith('.c') or file.endswith('.cxx')): processfile(petsc_dir,dir,file,keyre,mdict,uses)

def loadmanualpagescit(petsc_dir):
  '''Loads and parses the manualpages.cit file generated by Sowing doctext'''
  import re
  mdict = {}
  PATTERN = re.compile(r'man:\+(.*)\+\+(.*)\+\+\+\+man\+\.\./(.*)#.*')
  EXCLUDE_PATTERN = re.compile('PetscCall|Petsc[A-Z]*Int|PetscReal|PetscScalar|PetscBool|PetscComplex|PetscErrorCode|SETERR|PetscLog|PETSC_FALSE|PETSC_TRUE')
  with open(os.path.join(petsc_dir,'doc','manualpages','manualpages.cit'),'r') as fd:
    text = fd.read()
  for line in  text.split():
    m = re.match(PATTERN, line)
    # print('Manual page '+m.group(1)+' location '+m.group(3))
    if re.match(EXCLUDE_PATTERN,m.group(1)): continue
    mdict[m.group(1)] = m.group(3)
  # sort to find enclosing names first
  mdict = dict(sorted(mdict.items(), key=lambda item: len(item[0]), reverse = True))
  keyre = re.compile('|'.join(list(mdict.keys())))
  uses = {i: [] for i in mdict.keys()}
  return keyre,mdict,uses

def main(petsc_dir):
    keyre,mdict,uses = loadmanualpagescit(petsc_dir)
    for dirpath, dirnames, filenames in os.walk(os.path.join(petsc_dir,'src'),topdown=True):
      dirnames[:] = [d for d in dirnames if d not in ['output', 'ftn-custom', 'f90-custom', 'ftn-auto', 'f90-mod', 'tests', 'binding']]
      if dirpath.endswith('tutorials'):
        processdir(petsc_dir,dirpath,keyre,mdict,uses)

    for i in mdict:
      if len(uses[i]) > 0:
        manpage = os.path.join(petsc_dir,'doc','manualpages',mdict[i])
        with open(manpage,'a') as fd:
          fd.write('\n## Examples\n')
          for j in uses[i]:
            file = j.replace(petsc_dir+'/','')
            fd.write('<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/'+file+'.html">'+file+'</A><BR>\n')

if __name__ == "__main__":
   main(os.path.abspath(os.environ['PETSC_DIR']))
